package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {

    private EmployeeMapper employeeMapper;

    @Autowired
    public void setEmployeeMapper(EmployeeMapper employeeMapper) {
        this.employeeMapper = employeeMapper;
    }

    @Override
    public void delete(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Employee employee, Long[] ids) {
        //先保存员工
        employeeMapper.insert(employee);
        //再保存角色,判断是否有值
        if (ids != null && ids.length > 0) {
            for (Long rid : ids) {
                employeeMapper.insertByRid(employee.getId(), rid);
            }
        }
    }

    @Override
    public Employee get(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> listAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public void update(Employee employee, Long[] ids) {
        employeeMapper.updateByPrimaryKey(employee);
        //先删除中间表的数据
        employeeMapper.deleteByEid(employee.getId());
        //再关联关系
        if (ids != null && ids.length > 0) {
            for (Long rid : ids) {
                employeeMapper.insertByRid(employee.getId(), rid);
            }
        }

    }

    @Override
    public PageInfo<Employee> query(EmployeeQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Employee> employees = employeeMapper.selectForList(qo);
        return new PageInfo<>(employees);
    }

    //登录时找用户
    @Override
    public Employee login(String username, String password) {
        if (!StringUtils.hasText(username)) {
            throw new RuntimeException("用户名不能为空");
        }
        if (!StringUtils.hasText(password)) {
            throw new RuntimeException("密码不能为空");
        }
        Employee employee = employeeMapper.login(username, password);
        if (employee == null) {
            throw new RuntimeException("用户名和密码错误");
        }
        return employee;
    }

    @Override
    public List<String> selectByEidPermission(Long id) {
        return employeeMapper.selectByEidPermission(id);
    }

    @Override
    public Employee selectByUername(String username) {
        Employee e = employeeMapper.selectByUername(username);
        return e;
    }

    @Override
    public Workbook exportXls() {
        //创建个文本簿
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建个表
        HSSFSheet sheet = workbook.createSheet("员工名单");
        //创建第一行
        HSSFRow row = sheet.createRow(0);
        //设置标题
        row.createCell(0).setCellValue("姓名");
        row.createCell(1).setCellValue("用户名");
        row.createCell(2).setCellValue("密码");

        //查询员工
        List<Employee> employees = employeeMapper.selectAll();
        for (int i = 0; i < employees.size(); i++) {
            //获得员工
            Employee employee = employees.get(i);
            //设置
            row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(employee.getName());
            row.createCell(1).setCellValue(employee.getUsername());
            row.createCell(2).setCellValue(employee.getPassword());

        }
        return workbook;

    }

    @Override
    public void importXls(MultipartFile file) throws IOException {
        //获取整个工作簿
        HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());
        //获取表
        HSSFSheet sheetAt = workbook.getSheetAt(0);
        //获取最后一行数据
        int lastRowNum = sheetAt.getLastRowNum();
        for (int i = 1; i <= lastRowNum; i++) {
            HSSFRow row = sheetAt.getRow(i);
            System.out.println(row);
            Employee employee = new Employee();
            if (row.getCell(0).getStringCellValue() != null) {
                employee.setName(row.getCell(0).getStringCellValue());
            }
            if (row.getCell(1).getStringCellValue() != null) {
                employee.setUsername(row.getCell(1).getStringCellValue());
            }

            //设置默认密码1
            employee.setPassword("1");
            //保存
            employeeMapper.insert(employee);
        }
    }

    @Override
    public void updatePassword(Long id) {
        employeeMapper.updatePassword(id);
    }

    @Override
    public List<Employee> selectedAll() {
        return employeeMapper.selectAll();
    }
}
